/******************************************* 
 * 配置硬件初始化
*******************************************/

#include "air001_bsp.h"
#include "SoftTimer.h"
#include "key_device.h"
#include "i2c_aht10.h"


#define  PULSE1_VALUE       10


#if 1			// 串口
UART_HandleTypeDef UART2_Handle;

void UART_IO_MspInit(void)
{
	GPIO_InitTypeDef GPIOA_2_3_Init;
	__HAL_RCC_GPIOA_CLK_ENABLE();
	// TX
	GPIOA_2_3_Init.Pin = GPIO_PIN_4;
	GPIOA_2_3_Init.Mode = GPIO_MODE_AF_PP;
	GPIOA_2_3_Init.Pull = GPIO_PULLUP;
	GPIOA_2_3_Init.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	GPIOA_2_3_Init.Alternate = GPIO_AF9_USART2;
	HAL_GPIO_Init(GPIOA, &GPIOA_2_3_Init);
	// RX
	GPIOA_2_3_Init.Pin = GPIO_PIN_3;
	GPIOA_2_3_Init.Alternate = GPIO_AF4_USART2;
	HAL_GPIO_Init(GPIOA, &GPIOA_2_3_Init);
	HAL_NVIC_SetPriority(USART2_IRQn, 3, 3);
	HAL_NVIC_EnableIRQ(USART2_IRQn);
}

void UART2_BSP_Init(void)
{
	__HAL_RCC_USART2_CLK_ENABLE();
	UART2_Handle.Instance = USART2;
	UART2_Handle.Init.BaudRate = 115200;
	UART2_Handle.Init.WordLength = 8;
	UART2_Handle.Init.StopBits = 1;
	UART2_Handle.Init.Parity = UART_PARITY_NONE;
	UART2_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	UART2_Handle.Init.Mode = UART_MODE_TX_RX;
	HAL_UART_Init(&UART2_Handle);
}

int fputc(int ch, FILE *f)
{
	/* Send a byte to USART */
	HAL_UART_Transmit(&UART2_Handle, (uint8_t *)&ch, 1, 1000);

	return (ch);
}

int fgetc(FILE *f)
{
	int ch;
	HAL_UART_Receive(&UART2_Handle, (uint8_t *)&ch, 1, 1000);
	return (ch);
}

#endif

#if 1

#define PWM_PIN 		(GPIO_PIN_0)			// PWM信号绑定PIN_A0
#define SET_Alternate 	GPIO_AF13_TIM1	    	// 设置复用模式
 
#define PWM_Period  	500 - 1	  			// 自动重装载值 24M / 50k = 480
#define PWM_VALUE  		0					// 初始设置占空比
#define PWM_Prescaler 	1 - 1				// 预分频为 0

static void Error_Handler(void);

TIM_HandleTypeDef TimHandle = {0};

TIM_OC_InitTypeDef 	sConfig = {0};


void Init_Time1()
{
	/* 初始化GPIOA0的EN引脚 */
	__HAL_RCC_GPIOA_CLK_ENABLE();
	
	GPIO_InitTypeDef GPIO_PWM = {
		.Pin = PWM_PIN,										// A0
		.Mode = GPIO_MODE_AF_PP,							// 推挽复用模式
		.Pull = GPIO_NOPULL,								// 无上拉或下拉
		.Speed = GPIO_SPEED_FREQ_VERY_HIGH,					// 最高速度模式
		.Alternate = SET_Alternate,						// 复用模式 AF 2：连接到 TIM1
	};
	
	/* GPIOA时钟使能 */
	HAL_GPIO_Init(GPIOA, &GPIO_PWM);

	GPIO_InitTypeDef GPIO_PWM1 = {
		.Pin = GPIO_PIN_1,									// PA1
		.Mode = GPIO_MODE_AF_PP,							// 推挽复用模式
		.Pull = GPIO_NOPULL,								// 无上拉或下拉
		.Speed = GPIO_SPEED_FREQ_VERY_HIGH,					// 最高速度模式
		.Alternate = SET_Alternate							// 复用模式 AF 2：连接到 TIM1
	};
	
	/* GPIOA时钟使能 */
	HAL_GPIO_Init(GPIOA, &GPIO_PWM1);
	
	/* TIM1时钟使能 */
  __HAL_RCC_TIM1_CLK_ENABLE();
	
  /* 系统时钟配置 */
  //APP_SystemClockConfig(); 
	
  	TimHandle.Instance = TIM1;                                         	/* 选择TIM1 */
	TimHandle.Init.Prescaler         = PWM_Prescaler;                	/* 预分频 */
	TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;           	/* 向上计数 */
	TimHandle.Init.Period            = PWM_Period;                     	/* 自动重装载值 */
	TimHandle.Init.ClockDivision     = TIM_CLOCKDIVISION_DIV1;         	/* 时钟不分频 */
	TimHandle.Init.RepetitionCounter = 1 - 1;                          	/* 不重复计数 */
	TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; 	/* 自动重装载寄存器没有缓冲 */
  
	/* 基础时钟初始化 */
	if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
	{
		Error_Handler();
	}
	
	/* 配置通道3，因为PA0在TIM1_CH3 */
	sConfig.OCMode       = TIM_OCMODE_PWM1;                     /* 输出配置为PWM1 */
	sConfig.Pulse 	     = PWM_VALUE;                     		/* 设置占空比 */
	sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;                 /* OC通道输出高电平有效 */
	sConfig.OCFastMode   = TIM_OCFAST_DISABLE;                  /* 输出快速使能关闭 */
	sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;                /* OCN通道输出高电平有效 */
	sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;              /* 空闲状态OC1N输出低电平 */
	sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;               /* 空闲状态OC1输出低电平 */
	
	if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
	{
		Error_Handler();
	}

	/* 配置通道3，因为PA0在TIM1_CH3 */
	sConfig.OCMode       = TIM_OCMODE_PWM1;                     /* 输出配置为PWM1 */
	sConfig.Pulse 	   	 = PWM_VALUE;                     		/* 设置占空比 */
	sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;                 /* OC通道输出高电平有效 */
	sConfig.OCFastMode   = TIM_OCFAST_DISABLE;                  /* 输出快速使能关闭 */
	sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;                /* OCN通道输出高电平有效 */
	sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;              /* 空闲状态OC1N输出低电平 */
	sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;               /* 空闲状态OC1输出低电平 */
		
	if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
	{
		Error_Handler();
	}
	
	/* 开启所有通道输出 */
	if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
	{
		Error_Handler();
	}
	if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
	{
		Error_Handler();
	}
}

void Change_Pulse_N(int value, uint16_t change_N)
{
	switch (change_N)
	{
		case 1:
		{
			__HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_1, value);    //修改比较值，修改占空比
			break;
		}

		case 2:
		{
			__HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_2, value);    //修改比较值，修改占空比
			break;
		}

		case 3:
		{
			__HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_3, value);    //修改比较值，修改占空比
			break;
		}

		case 4:
		{
			__HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_4, value);    //修改比较值，修改占空比
			break;
		}
			
		default:
		{
			break;
		}
	}
	
}

 
/**
  * @brief   错误执行函数
  * @param   无
  * @retval  无
  */
static void Error_Handler(void)
{
  /* 无限循环 */
  while (1)
  {
  }
}



#endif

#if 0			// 定时器 TIM1
TIM_HandleTypeDef    Tim_1_Handle;

static void TIM1_BSP_Init(void)
{
	TIM_OC_InitTypeDef sConfig;

	__HAL_RCC_TIM1_CLK_ENABLE();

	Tim_1_Handle.Instance = TIM1;                                                  /* 选择TIM1 */
	Tim_1_Handle.Init.Period            = 100;                                     /* 自动重装载值 */
	Tim_1_Handle.Init.Prescaler         = 800 - 1;                                 /* 预分频为800-1 */
	Tim_1_Handle.Init.ClockDivision     = TIM_CLOCKDIVISION_DIV1;                  /* 时钟不分频 */
	Tim_1_Handle.Init.CounterMode       = TIM_COUNTERMODE_UP;                      /* 向上计数 */
	Tim_1_Handle.Init.RepetitionCounter = 1 - 1;                                   /* 不重复计数 */
	Tim_1_Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;          /* 自动重装载寄存器没有缓冲 */

	/* 基础时钟初始化 */
	HAL_TIM_PWM_Init(&Tim_1_Handle);
	
	sConfig.OCMode       = TIM_OCMODE_PWM1;                                     /* 输出配置为PWM1 */
	sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;                                 /* OC通道输出高电平有效 */
	sConfig.OCFastMode   = TIM_OCFAST_ENABLE;                                   /* 输出快速使能关闭 */
	sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;                                /* OCN通道输出高电平有效 */
	sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;                              /* 空闲状态OC1N输出低电平 */
	sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;                               /* 空闲状态OC1输出低电平 */
	sConfig.Pulse = PULSE1_VALUE;                                               /* CC1值为10，占空比=10/50=20% */
	
	/* 配置通道1 */
	HAL_TIM_PWM_ConfigChannel(&Tim_1_Handle, &sConfig, TIM_CHANNEL_1);

	/* 开启所有通道输出 */
	HAL_TIM_PWM_Start(&Tim_1_Handle, TIM_CHANNEL_1);

}
#endif

#if 0			// 定时器 TIM14
TIM_HandleTypeDef    Tim_14_Handle;

static void TIM14_BSP_Init(void)
{
	TIM_OC_InitTypeDef sConfig;

	__HAL_RCC_TIM1_CLK_ENABLE();
	Tim_14_Handle.Instance = TIM1;                                                  /* 选择TIM1 */
	Tim_14_Handle.Init.Period            = 50;                                      /* 自动重装载值 */
	Tim_14_Handle.Init.Prescaler         = 800 - 1;                                 /* 预分频为800-1 */
	Tim_14_Handle.Init.ClockDivision     = TIM_CLOCKDIVISION_DIV1;                  /* 时钟不分频 */
	Tim_14_Handle.Init.CounterMode       = TIM_COUNTERMODE_UP;                      /* 向上计数 */
	Tim_14_Handle.Init.RepetitionCounter = 1 - 1;                                   /* 不重复计数 */
	Tim_14_Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;          /* 自动重装载寄存器没有缓冲 */

	/* 基础时钟初始化 */
	if (HAL_TIM_PWM_Init(&Tim_14_Handle) != HAL_OK)
	{
//		Error_Handler();
	}
	
	sConfig.OCMode       = TIM_OCMODE_PWM1;                                     /* 输出配置为PWM1 */
	sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;                                 /* OC通道输出高电平有效 */
	sConfig.OCFastMode   = TIM_OCFAST_DISABLE;                                  /* 输出快速使能关闭 */
	sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;                                /* OCN通道输出高电平有效 */
	sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;                              /* 空闲状态OC1N输出低电平 */
	sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;                               /* 空闲状态OC1输出低电平 */

	sConfig.Pulse = PULSE1_VALUE;                                               /* CC1值为10，占空比=10/50=20% */
	/* 配置通道1 */
	if (HAL_TIM_PWM_ConfigChannel(&Tim_14_Handle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
	{
//		Error_Handler();
	}
	/* 开启所有通道输出 */
	if (HAL_TIM_PWM_Start(&Tim_14_Handle, TIM_CHANNEL_1) != HAL_OK)
	{
//		Error_Handler();
	}
}

#endif

#if 0 			// 定时器 TIM3

TIM_HandleTypeDef    TimHandle;
void TIM3_BSP_Init(void)
{
	TimHandle.Instance = TIM3;                                          
    TimHandle.Init.Period            = 3200 - 1;                        
    TimHandle.Init.Prescaler         = 100 - 1;                        
    TimHandle.Init.ClockDivision     = TIM_CLOCKDIVISION_DIV1;          
    TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;              
    TimHandle.Init.RepetitionCounter = 1 - 1;                           
    TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;  

    HAL_TIM_Base_Init(&TimHandle);

    HAL_TIM_Base_Start_IT(&TimHandle);
}

#endif

#if 1			// AD

/* Private define ------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef             AdcHandle;
ADC_ChannelConfTypeDef        AD_sConfig;
// volatile int16_t              aADCxConvertedData;
// int16_t                       aTEMPERATURE;

/**
  * @brief  ADC配置函数
  * @param  无
  * @retval 无
  */
static void APP_AdcConfig(void)
{
  __HAL_RCC_ADC_FORCE_RESET();
  __HAL_RCC_ADC_RELEASE_RESET();                                         /* 复位ADC */
  __HAL_RCC_ADC_CLK_ENABLE();                                            /* ADC时钟使能 */
  
  AdcHandle.Instance = ADC1;
  /* ADC校准 */
  if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK)                 
  {
    Error_Handler();
  }
  
  AdcHandle.Instance = ADC1;
  AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;              /* 设置ADC时钟 */
  AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;                        /* 转换分辨率12bit */
  AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;                        /* 数据右对齐 */
  AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;              /* 扫描序列方向：向上 */
  AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;                     /* 单次采样 */
  AdcHandle.Init.LowPowerAutoWait = ENABLE;                              /* 等待转换模式开启 */
  AdcHandle.Init.ContinuousConvMode = DISABLE;                           /* 单次转换模式 */
  AdcHandle.Init.DiscontinuousConvMode = DISABLE;                        /* 不使能非连续模式 */
  AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;                  /* 软件触发 */
  AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;   /* 触发边沿无 */
  AdcHandle.Init.DMAContinuousRequests = DISABLE;                        /* 不使能DMA */
  AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;                     /* 当过载发生时，覆盖上一个值 */
  AdcHandle.Init.SamplingTimeCommon=ADC_SAMPLETIME_239CYCLES_5;           /* 设置采样周期 */

  /* 初始化ADC */
  if (HAL_ADC_Init(&AdcHandle) != HAL_OK)                                
  {
    Error_Handler();
  }

  AD_sConfig.Channel = ADC_CHANNEL_2;                                       /* ADC通道选择 */
  AD_sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;                                /* 设置加入规则组通道 */
  /* 配置ADC通道 */
  if (HAL_ADC_ConfigChannel(&AdcHandle, &AD_sConfig) != HAL_OK)             
  {
    Error_Handler();
  }
  
  AD_sConfig.Channel = ADC_CHANNEL_6;                                       /* ADC通道选择 */
  AD_sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;                                /* 设置加入规则组通道 */
  /* 配置ADC通道 */
  if (HAL_ADC_ConfigChannel(&AdcHandle, &AD_sConfig) != HAL_OK)             
  {
    Error_Handler();
  }
  
	AD_sConfig.Rank         = ADC_RANK_CHANNEL_NUMBER;                                 /* 设置加入规则组通道 */
	AD_sConfig.Channel      = ADC_CHANNEL_TEMPSENSOR;                                  /* 设置采样通道 */
  /* 配置ADC通道 */
  if (HAL_ADC_ConfigChannel(&AdcHandle, &AD_sConfig) != HAL_OK)             
  {
    Error_Handler();
  }
}

uint32_t adc_value[4];

void AD_deal_100ms(void)
{
    /* ADC开启 */  
    HAL_ADC_Start(&AdcHandle);    

    for (uint8_t i = 0; i < 3; i++)
    {
        /* 等待ADC转换 */
        HAL_ADC_PollForConversion(&AdcHandle, 10000); 
        /* 获取AD值 */
        adc_value[i] = HAL_ADC_GetValue(&AdcHandle);   
    }
}


#endif


#if 1

// IIC 功能






#endif

void AIR001_BSP_INIT(void)
{
	KeyDeviceInit();
	
	UART_IO_MspInit();
	UART2_BSP_Init();
	
	Init_Time1();
	APP_AdcConfig();

	oled_I2C_init();
	HAL_OLED_Init();
	oled_clear();


}


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	
}


